// Copyright (c) 2010, Anthony Cassidy
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are 
// permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice, this list 
//         of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright notice, this 
//         list of conditions and the following disclaimer in the documentation and/or 
//         other materials provided with the distribution.
//     * Neither the name of the AntsAdventureGameCreator nor the names of its contributors 
//         may be used to endorse or promote products derived from this software without 
//         specific prior written permission.
// 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
// THE POSSIBILITY OF SUCH DAMAGE.
#include "XStdafx.h"
#include "XInventorySlotCollection.h"

#include "XDiagnostics.h"



XInventorySlotCollection::XInventorySlotCollection()
{
}

XInventorySlotCollection::~XInventorySlotCollection()
{
    XDiagnostics::Delete("XInventorySlotCollection");
}

void XInventorySlotCollection::Clear()
{ 
    m_rects.clear();
}

int XInventorySlotCollection::Count()
{ 
    return m_rects.size();
}


void XInventorySlotCollection::DrawSlotIfVisible(int i, QPainter& displayPainter, QPainter& zbufferPainter, QRgb zbufferColor)
{
    displayPainter.drawImage(0,0,m_images[i]);
    zbufferPainter.fillRect( m_rects[i], QBrush(zbufferColor));
}

bool XInventorySlotCollection::IsMouseOverSlot(int i, QPoint pos)
{
    return m_rects[i].contains(pos);
}


void XInventorySlotCollection::TriggerSlot(int index)
{
    (index=index);
}

void WipeCanvas(QImage& image);



QImage  GenerateImageForInventory(QImage bigSquareInv, QRect rect)
{
    QImage image(rect.right(), rect.bottom(), QImage::Format_ARGB32_Premultiplied);

    //always wipe canvas before painter
    WipeCanvas(image);

    QPainter painter;
    bool isOk = painter.begin(&image);
    if(isOk)
    {
        painter.fillRect(rect, Qt::black);
        painter.drawImage(rect, bigSquareInv, QRectF(0,0,bigSquareInv.width(), bigSquareInv.height()));
        bool isOk2 = painter.end();
        (isOk2=isOk2);
        ASSERT(isOk2);
    }
    return image;
}

void XInventorySlotCollection::UpdateSlotImage(int i, QImage image)
{
    QImage resizedImage = GenerateImageForInventory(image, m_rects[i]);
    int j = i;
    if(m_images.size()<=j)
        m_images.resize(i+1);

    m_images[i] = resizedImage; 
}


void XInventorySlotCollection::ResizeRectList(QRect rect, QRect& rectForLeftArrow, QRect& rectForRightArrow)
{

    const int minimumArrowWidthIsFive =5;
    const int minimumPictogramWidthIsTen =40;

    // initialize image 


    // arrows are good to have at about 1/10th of the width of the inventory.
    int arrowWidth = rect.width()/10;
    // but they can't go below a minimum size.
    if(arrowWidth < minimumArrowWidthIsFive)
        arrowWidth =5;
    if(rect.width()/2 < arrowWidth + minimumPictogramWidthIsTen)
        return;

    int visiblePictograms = 0;
    int lengthForPictograms = rect.width() - 2*arrowWidth;
    if(lengthForPictograms>=minimumPictogramWidthIsTen*5 && lengthForPictograms/5 < rect.height())
    {
        visiblePictograms = 5;
    }
    else if(lengthForPictograms>=minimumPictogramWidthIsTen*4 && lengthForPictograms/4 < rect.height())
    {
        visiblePictograms = 4;
    }
    else if(lengthForPictograms>=minimumPictogramWidthIsTen*3 && lengthForPictograms/3 < rect.height())
    {
        visiblePictograms = 3;
    }
    else if(lengthForPictograms>=minimumPictogramWidthIsTen*2 && lengthForPictograms/4 < rect.height())
    {
        visiblePictograms = 2;
    }
    else // too small! 
    {
        return;
    }

    int resultantSideLengthOfPictogram = lengthForPictograms/visiblePictograms;
    int resultantArrowButtonWidth = (rect.width()-resultantSideLengthOfPictogram*visiblePictograms)/2;
    int resultantRowHeight = (int)(resultantSideLengthOfPictogram /1.778); //magic number, sheesh!


    // finally, set the rectangles
    int x = rect.x();
    int y = rect.y();
    int startingXCoord = x + resultantArrowButtonWidth;
    rectForLeftArrow = QRect(x, y, resultantArrowButtonWidth, resultantRowHeight); 

    int lastRightCoord = startingXCoord;

    m_rects.clear();
    for(int i=0;i<visiblePictograms;i++)
    {
        QRect rect(lastRightCoord+1,y,resultantSideLengthOfPictogram, resultantRowHeight);
        m_rects.push_back(rect);
        lastRightCoord = lastRightCoord + resultantSideLengthOfPictogram;
    }

    rectForRightArrow = QRect(lastRightCoord+1,y,resultantArrowButtonWidth,resultantRowHeight);
}
